package com.botao.tip.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.botao.tip.entity.LoginCount;
import com.botao.tip.entity.User;
import com.botao.tip.service.IUserService;
import com.botao.tip.service.LoginCountService;
import com.botao.tip.utils.AjaxResult;
import com.botao.tip.utils.TokenUtil;
import com.botao.tip.convert.UserConvert;
import com.botao.tip.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @author cbt28
 */
@RestController
@RequestMapping("/api")
public class BaseController {
    @Autowired
    private IUserService userService;

    @PostMapping("login")
    public AjaxResult login(@RequestParam("username") String username, @RequestParam("password") String password) {
        if ("".equals(username) || username == null || "".equals(password) || password == null) {
            return AjaxResult.error("用户名或密码不能为空！");
        }
        User user = userService.login(username, password);
        if (user == null) {
            return AjaxResult.error("用户名或密码不正确！");
        }
        if (user.getStatus() != 0) {
            return AjaxResult.error("该账户已被停用！");
        }

        String token = TokenUtil.createToken(user.getId(),
                user.getUsername(),
                user.getRole());
        UserVo userVo = UserConvert.convertToUserVo(user);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("token", token);
        map.put("user", userVo);

        return AjaxResult.success(map);


    }

    @RequestMapping("err")
    public AjaxResult tokenError() {
        return AjaxResult.error("请求被拦截！");
    }
}
